/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idea.model.powersearch;


import idea.ipreview.IPreviewTopComponent;
import idea.model.dbmanager.QueryManager;

import idea.model.qnrlibrary.CustomContentItemSearchView;
import idea.model.qnrlibrary.QuestionTitle;
import java.awt.event.ActionEvent;
import java.beans.IntrospectionException;
import java.util.Iterator;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import static javax.swing.Action.NAME;
import javax.swing.ImageIcon;
import org.openide.nodes.BeanNode;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.lookup.Lookups;
import org.openide.windows.WindowManager;


/**
 *
 * @author rejun001
 */
public class CCISearchResultsChildFactory extends ChildFactory<CustomContentItemSearchView>
{

    private List<CustomContentItemSearchView> resultList;


    public CCISearchResultsChildFactory( List<CustomContentItemSearchView> resultList )
    {
        this.resultList = resultList;
    }


    @Override
    protected boolean createKeys( List<CustomContentItemSearchView> list )
    {
        list.removeAll( list );
        for ( CustomContentItemSearchView c : resultList )
        {
            list.add( c );
        }
        return true;
    }


    @Override
    protected Node createNodeForKey( CustomContentItemSearchView key )
    {
        CCISearchResultsNode node = null;
        try
        {
            node = new CCISearchResultsNode( key );
        }
        catch ( Exception e )
        {
            Exceptions.printStackTrace( e );
        }
        return node;
    }


    private class CCISearchResultsNode extends BeanNode
    {

        private String currSurvey = null;
        private String currRefPeriod = null;
        private String currQuestId = null;
        private String currQuestionTitle = null;
        private String currCategory = null;


        public CCISearchResultsNode( CustomContentItemSearchView bean ) throws IntrospectionException
        {
            super( bean , Children.LEAF , Lookups.singleton( bean ) );
            currSurvey = bean.getSurvey();
            currRefPeriod = bean.getRefPeriod();
            currQuestId = bean.getQuestId();
            currQuestionTitle = bean.getQuestionTitle();
            currCategory = bean.getCategory();

            setDisplayName( bean.getSurvey() + ": " + bean.getRefPeriod() );
            setShortDescription( bean.getItemWording() );
        }


        /**
         * Context menu. On the Node, the original Action is pulling up a
         * properties window. On a field, the original Action is a WHERE clause
         * choice. The other two (Designer and DrillDown) are added here.
         *
         * @param bln -- bln tells whether or not it's a context menu.
         * @return the array of actions for the Node.
         */
        @Override
        public Action[] getActions( boolean bln )
        {
            Action[] oldActions = super.getActions( bln );
            Action[] newActions = new Action[ oldActions.length + 2 ];
            System.arraycopy( oldActions , 0 , newActions , 0 , oldActions.length );
            newActions[oldActions.length] = new ShowPreviewAction();
            newActions[oldActions.length + 1] = new SameContentQuestionsAction();
            //newActions[oldActions.length + 1] = new DrillDownAction();
            return newActions;

        }


        /**
         * The Action for searching for questions with identical content (category + questionTitle) to this
         * 
         * @see AbstractAction
         */
        private class SameContentQuestionsAction extends AbstractAction
        {
            public SameContentQuestionsAction()
            {
                putValue(NAME, "Get Questions with this Content");
            }


            @Override
            public void actionPerformed( ActionEvent e )
            {
                CCISearchResultsTopComponent stc = (CCISearchResultsTopComponent) WindowManager.getDefault()
                        .findTopComponent( "CCISearchResultsTopComponent");
                List<CustomContentItemSearchView> resultList = QueryManager.getSameContentQuestions( currSurvey, currRefPeriod, currCategory, currQuestionTitle );
                stc.displayCCISearchResults(resultList);
                //stc.displaySameContentQuestions(currSurvey, currRefPeriod, currCategory, currQuestionTitle);
                stc.open();
            }
        }
        /**
         * The Action for bringing up the SFO window
         * 
         * @see AbstractAction
         */
        private class ShowPreviewAction extends AbstractAction //implements Presenter.Popup
        {

            /**
             * xtor. The label for the context menu item.
             */
            public ShowPreviewAction()
            {
                putValue( NAME , "Show Preview" );
            }


            /**
             * the action performed.
             *
             * @param e
             */
            @Override
            public void actionPerformed( ActionEvent e )
            {

                ImageIcon jpeg = getJpeg( currQuestId );
                IPreviewTopComponent prevtc = (IPreviewTopComponent) WindowManager.getDefault()
                        .findTopComponent( "IPreviewTopComponent" );
                prevtc.displayPreview( jpeg , "My Preview" );
                prevtc.open();
                prevtc.requestActive();
            }


            /**
             *
             * @param questID
             * @return
             */
            public ImageIcon getJpeg( String questID )
            {
                ImageIcon image = null;
                List<QuestionTitle> qts = QueryManager.getQT( questID );
                QuestionTitle qt = null;
                Iterator<QuestionTitle> qtit = qts.iterator();
                while ( qtit.hasNext() )
                {
                    qt = qtit.next();
                    break;
                }
                image = QueryManager.getJpegFromDB( qt );

                return image;
            }

        }

    }

}
